package com.uzai.console.service.tb.tbapi.impl;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkScTpwdConvertRequest;
import com.taobao.api.response.TbkScTpwdConvertResponse;
import com.uzai.console.common.web.interceptor.UserInfoFilter;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.tb.tbapi.TbkScTpwdConvertDto;
import com.uzai.console.service.tb.tbapi.TbApiService;
import com.uzai.console.vo.tb.tbapi.TbkScTpwdConvertVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2022年11月25日 14:16
 */
@Service
public class TbApiServiceImpl implements TbApiService {

    private Logger logger = LoggerFactory.getLogger(TbApiServiceImpl.class);

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 淘宝客-服务商-淘口令解析&转链
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbkScTpwdConvertDto
     * @return null
     */
    public TbkScTpwdConvertVo tbkScTpwdConvert(TbkScTpwdConvertDto tbkScTpwdConvertDto){
        TbkScTpwdConvertVo tbkScTpwdConvertVo = new TbkScTpwdConvertVo();
        String tbApiUrl = uzaiConsoleSysConfig.getTbApiUrl();
        String tbAppkey = uzaiConsoleSysConfig.getTbAppkey();
        String tbAppsecret = uzaiConsoleSysConfig.getTbAppsecret();
        TaobaoClient client = new DefaultTaobaoClient(tbApiUrl, tbAppkey, tbAppsecret);
        TbkScTpwdConvertRequest req = new TbkScTpwdConvertRequest();
        req.setPasswordContent(tbkScTpwdConvertDto.getPasswordContent());
        req.setAdzoneId(tbkScTpwdConvertDto.getAdzoneId());
        req.setDx("1");
        req.setSiteId(tbkScTpwdConvertDto.getSiteId());
        try {
            TbkScTpwdConvertResponse rsp = client.execute(req, tbkScTpwdConvertDto.getSessionKey());
            TbkScTpwdConvertResponse.MapData data = rsp.getData();
            if(rsp.getData() != null){
                tbkScTpwdConvertVo.setNumIid(data.getNumIid());
                tbkScTpwdConvertVo.setClickUrl(data.getClickUrl());
                tbkScTpwdConvertVo.setOriginPid(data.getOriginPid());
                tbkScTpwdConvertVo.setOriginUrl(data.getOriginUrl());
                tbkScTpwdConvertVo.setSellerId(data.getSellerId());
            }
            System.out.println(rsp.getBody());

        } catch (Exception e) {
            logger.error("--error={}",e);
        }

        return tbkScTpwdConvertVo;
    }




}
